package com.nokia.ion.sdu.cdr_decoder;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CsvExporter {

    private FileWriter fw;

    public CsvExporter(File csvFile) throws IOException {
        fw = new FileWriter(csvFile, false);
    }

    public void write(Cdr cdr) throws IOException {
        String sLoc = "";
        switch (cdr.sgsnLoc) {
            case 1:
                sLoc = "LIAONING";
                break;
            case 2:
                sLoc = "NEIGHBOUR";
                break;
            case 3:
                sLoc = "OTHER";
                break;
        }
        String content = String.format("%s,%s,%s,%x,%d,%s,%d,%d\n", cdr.nodeID, cdr.servingNodeAddress, sLoc,
                cdr.servedIMSI, cdr.chargingId, Cdr.localFormat.format(cdr.recordOpeningTime), cdr.duration, cdr.causeForRecClosing);
        fw.write(content);
    }

    public void newLine() throws IOException {
        fw.write("\n");
    }

    public void newHeader() throws IOException {
        fw.write("node_id,s_addr,s_location,imsi,charging_id,open_time,duration,cause_for_close");
    }

    public void close() {
        try {
            fw.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}
